<?php
namespace  App\Http\Controllers\Radius;
use  App\Http\Controllers\Controller;

use App\Order;
use App\Rad_user;
use App\Rad_user_account;
use Illuminate\Http\Request;

use App\Common\ErrorDefine;
use App\Radacct;
use App\Radcheck;
use App\Radgroupcheck;
use App\Radreply;
use App\Radusergroup;
use App\Rad_manage;
use App\Radgroupreply;
use App\Rad_users;
use App\Rad_account;
use App\Rad_merge_data;
use DB;
use App\Rad_manage_discount;
use App\Rad_order;
use App\Line;
use App\Agent;

use App\Manage_user_list;
use App\Http\Controllers\VerifyCodeController;
use Redis;

class ModuleController extends Controller
{

    var $redis_;

    const VC_EXPIRE_TIME = 60;	//验证码过期时间（单位秒）



    function __construct()
    {
        $this->redis_ = null;

    }


    function __destruct()
    {
        if($this->redis_ != null) {
            $this->redis_->close();
        }
    }
    static function DeleteAccount($id)
    {


        $user = Radcheck::find($id);
        //   退款
        $ret =  Rad_users::find($id);


        if($user != NULL && $ret != NULL)
        {
            //  删除用户表信息
            $ret = Radcheck::where('username',$user->username)->delete();
            $ret1 = Radusergroup::where('username',$user->username)->delete();
            $ret2 = Rad_users::where('user_id',$id)->delete();
            $ret3 = Radreply::where('username',$user->username)->delete();
        }



    }




    /**
     * @param $request
     * 检测参数
     */
    static function CheckParamData($request)
    {
        $where = [];
        //  判断数据是否为空
        if(empty($request->all()))
        {
            return self::ShowError(ErrorDefine::EMPTY_POST_DATA);
        }

        if(!$request->has('token') || empty($request->input('token')))
        {
            return self::ShowError(ErrorDefine::INVALID_PARAMS);
        }

        if($request->has('username') && !empty($request->input('username')) &&  strlen($request->input('username')) < 20 && strlen($request->input('username')) >  0)
        {
            $where['username'] = $request->input('username');

        }else{
            return self::ShowError(ErrorDefine::INVALID_USERNAME);

        }


        if($request->has('password') && !empty($request->input('password')) && strlen($request->input('password')) > 0 && strlen($request->input('password')) < 20 )
        {
            $where['password'] = $request->input('password');

        }else{

            return self::ShowError(ErrorDefine::PASSWORD_NOT_CORRECT);
        }


        if($request->has('middle-day') &&  !empty($request->input('middle-day')) && $request->input('middle-day') > 0  )
        {
            $where['middle-day'] = $request->input('middle-day');

        }

        if($request->has('middle-week') && !empty($request->input('middle-week')) && $request->input('middle-week') > 0 )
        {
            $where['middle-week'] = $request->input('middle-week');
        }

        if($request->has('middle-month') && !empty($request->input('middle-month')) && $request->input('middle-month') > 0 )
        {
            $where['middle-month'] = $request->input('middle-month');

        }


        if($request->has('height-day') && !empty($request->input('height-day')) && $request->input('height-day') > 0 )
        {
            $where['height-day'] = $request->input('height-day');

        }


        if($request->has('height-week') && !empty($request->input('height-week')) && $request->input('height-week') > 0 )
        {
            $where['height-week'] = $request->input('height-week');

        }


        if($request->has('height-month') && !empty($request->input('height-month')) && $request->input('height-month') > 0 )
        {
            $where['height-month'] = $request->input('height-month');

        }


        if($request->has('level') && !empty($request->input('level')) && $request->input('level') > 0 )
        {
            $where['level'] = $request->input('level');
        }


        return $where;
    }


    static function CheckAccountParams($request)
    {

        $where = [];
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::EMPTY_POST_DATA);
            return;
        }



        //  名称
        if(!$request->has('username') || empty($request->input('username')) )
        {
            self::ShowError(ErrorDefine::NAME_NOT_EXIST);
            return;
        }else{
            $where['username'] = trim($request->input('username'));

        }


        //  密码
        if(!$request->has('password') || empty($request->input('password')))
        {
            self::ShowError(ErrorDefine::SHORT_OF_PASSWORD);
            return;
        }else{
            $where['password'] =  trim($request->input('password'));

        }


        //  账号类型
        if(!$request->has('account_type') || empty($request->input('account_type')))
        {
            self::ShowError(ErrorDefine::ACCOUNT_TYPE_ONT_EXIST);
            return;
        }else{
            $where['account_type'] = $request->input('account_type');
        }


        //  连接数
        if(!$request->has('concent_num') || empty($request->input('concent_num')) )
        {
            self::ShowError(ErrorDefine::CONCENT_NOT_EXIST);
            return;
        }else{
            $where['concent_num'] = $request->input('concent_num');
        }

        //   检测连接数
        if($where['concent_num']  <=0 )
        {
            self::ShowError(ErrorDefine::ILLEGAL_CONNECT_NUM);
            return;
        }


        //    备注
        $remark = '';
        if($request->input('remark'))
        {
            $where['remark']  = trim($request->input('remark'));
        }

        //    agent.niumoip.com 平台标识
        $agent = 0;
        if($request->input('agent'))
        {
            $where['agent'] = $request->input('agent');
        }


        $manage = 0;
        if($request->input('manage'))
        {
            $where['manage'] = $request->input('manage');
        }

        //  订单号
        if(!$request->has('order_id') || empty($request->input('order_id')))
        {
            self::ShowError(ErrorDefine::ORDER_NO_NOT_EXIST);
            return;
        }else{
            $where['order_id'] = $request->input('order_id');

        }


        if($where['account_type'] != '测试')
        {

            //   日期卡类型
            if(!$request->has('date_type') || empty($request->input('date_type')))
            {
                self::ShowError(ErrorDefine::DATE_TYPE_NOT_EXIST);
                return;
            }else{
                $where['date_type'] =  $request->input('date_type');
            }


            //     开通时间
            if(!$request->has('date') || empty($request->input('date')))
            {
                self::ShowError(ErrorDefine::DATE_NOT_EXIST);
                return;
            }else{
                $where['date'] = $request->input('date');
            }

            if($where['date']<=0) {
                self::ShowError(ErrorDefine::INVALID_DATE);
                return;
            }


            // 价格
            if(!$request->has('price') || empty($request->input('price')))
            {
                self::ShowError(ErrorDefine::PEICE_NOT_EXIST);
                return;
            }else{
                $where['price'] = $request->input('price');

            }

        }else{


            //   二级代理 普通用户 免费测试号 配额检测
            if($users[0]['level'] != 2 && $users[0]['level'] != 3 )
            {
                if($where['concent_num'] > $users[0]['test_con_max_quota'] )
                {
                    self::ShowError(ErrorDefine::TEST_ACCOUNT_LESS);
                    return;
                }


                if($users[0]['test_con_max_quota'] <= 0 || $users[0]['test_con_max_quota'] <= 0)
                {
                    self::ShowError(ErrorDefine::TEST_ACCOUNT_MORE);
                    return;
                }

            }

        }

//           用户类型   1   是普通用户  暂时默认普通
        if(!$request->has('set-meal-type') && empty($request->input('set-meal-type')))
        {
            self::ShowError(ErrorDefine::USER_TYPE_NO_EXIST);
            return;
        }else{
            $where['set-meal-type'] = $request->input('set-meal-type');
        }


        switch( $where['set-meal-type'])
        {
            case 2: $where['vip'] = 1;break;
            case 3:$where['vip'] = 2;break;
        }

       return $where;

    }


    /**
     * @param $username
     * 检测用户名是否存在
     */
    static function CheckUserExist($username)
    {
        $user = Radcheck::where('username',$username)->get(['id','username'])->toArray();
        if(!empty($user))
        {
            self::ShowError(ErrorDefine::USER_IS_EXIST);
            return;
        }
    }



    /**
     * @param $request
     *检测用户名
     */
    static function CheckUsername($request)
    {
        if(!$request->has('username') || empty($request->input('username')) || strlen($request->input('username')) > 20 || strlen($request->input('username')) <=  0)
        {
            return self::ShowError(ErrorDefine::INVALID_USERNAME);
        }
    }


    /**
     * @param $request
     *检测密码
     */
    static function CheckPassword($request)
    {
        if(!$request->has('password') || empty($request->input('password')) || strlen($request->input('password')) > 20 || strlen($request->input('password')) <=  0)
        {
            return self::ShowError(ErrorDefine::INVALID_USERNAME);
        }
    }


    /**
     * @param $request
     * 检测用户token是否存在
     */
    static function CheckUserToken($request)
    {
        if(!$request->has('token') || empty($request->input('token')))
        {
            return self::ShowError(ErrorDefine::INVALID_PARAMS);
        }
    }





    /**
     * @param $token
     * @return mixed
     * token   换取 用ID
     */
    static function getTokenById($token)
    {
        $user = Rad_manage::where('token',$token)->get()->toArray();
        return $user;
    }




    /**
     * @param $user
     * @param $where
     * 二级代理的开户优惠价格检测
     */
    static function CheckSetMealPrice($user,$where,$id)
    {
        if($user[0]['middle-day'] > $where['middle-day'])
        {
            return self::ShowError(ErrorDefine::INVALID_BSAIC_DAY);
        }


        if($user[0]['middle-week'] > $where['middle-week'])
        {
            return self::ShowError(ErrorDefine::INVALID_BSAIC_WEEK);
        }

        if($user[0]['middle-month'] > $where['middle-month'])
        {
            return self::ShowError(ErrorDefine::INVALID_BSAIC_MONTH);
        }

        if($user[0]['height-day'] > $where['height-day'])
        {
            return self::ShowError(ErrorDefine::INVALID_HEIGHT_DAY);
        }

        if($user[0]['height-week'] > $where['height-week'])
        {
            return self::ShowError(ErrorDefine::INVALID_HEIGHT_WEEK);
        }

        if($user[0]['height-month'] > $where['height-month'])
        {
            return self::ShowError(ErrorDefine::INVALID_HEIGHT_MONTH);
        }


        $manage = Rad_manage::where('manage_id',$id)->first();

        if($manage == NULL)
        {
            return self::ShowError(ErrorDefine::INVALID_PARAMS);
        }

        if($manage->belong_manage != $user[0]['manage_id'])
        {
            return self::ShowError(ErrorDefine::INVALID_PARAMS);
        }


    }






}